--[[
	
Title: inherit
Author(s): cool night
Lua version: 5.1
Date: 2021/04/4
Desc: 
    inherit and multi_inherit

--]]

function inherit(baseClass, newClass, ctor)
    assert(baseClass and type(baseClass) == "table", "Invalid param baseClass.")
    assert((not newClass) or (type(newClass) == "table"), "Invalid param newClass.")
    assert((not ctor) or (type(ctor) == "function"), "Invalid param ctor.")

    newClass = newClass or {}
    setmetatable(newClass, {__index = baseClass})
    newClass._super = baseClass

    function newClass:ctor()
        if baseClass.ctor and type(baseClass.ctor) == "function" then
            baseClass.ctor(self)
        end

        if ctor and type(ctor) == "function" then
            ctor(self) 
        end
    end

    function newClass:new(o)
        o = o or {}
        setmetatable(o, {__index = newClass})

        self.ctor(o)

        return o
    end

    return newClass
end

function multi_inherit(...)
    local arg = {...}

    local newClass = {}
    setmetatable(newClass, {__index = function(new_class, new_key)
        local new_value
        for k, v in ipairs(arg) do
            new_value = v[new_key]
            if new_value then break end
        end

        new_class[new_key] = new_value
        return new_value
    end})

    function newClass:new(o)
		o = o or {}
		setmetatable(o, {__index = newClass})
		return o
    end
    
    return newClass
end